/**
 * 
 */
package com.myworld.config.mq.rocket.consumer;

import java.util.Date;
import java.util.List;

import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/** 
 *
 */
@Component("jFMessagesListener")  //发送消息测试时注释掉该类
public class JFMessagesListener implements MessageListenerConcurrently {
	static final Logger logger = LoggerFactory.getLogger(JFMessagesListener.class);

	@Override
     public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
		int i=0;            
		for(MessageExt ext:msgs){
			try {
				Thread.sleep(1000);
				/**
				 * MessageExt说明：
				 *  String getTags() ：获得tag的值  ==>一般可以用来对消息进行区分 处理
				 *  int  getReconsumeTimes():获得该消息是第几次被消费，常用于指定消息消费的次数
				 */
				//业务代码写在此处
				System.out.println(new Date()+new String(ext.getBody(),"UTF-8"));
			} catch (Exception e) {
				e.printStackTrace();
				//告知Broker消息消费失败，消息回到消息队列
				return ConsumeConcurrentlyStatus.RECONSUME_LATER;
			}                
		}
		//消息消费成功，删除消息
		return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
	};

}

